package jh.poj.builder;

import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

public class HTMLDocumentBuilder implements DocumentBuilder {

	StringBuffer html;
	StringBuffer body;
	StringBuffer title;
	StringBuffer author;
	private int currentChapterLevel;
	private final ChaptersNumering mChaptersNumering;
	private final String chapterLevelStartTag;
	private final String chapterLevelEndTag="</OL>\n";
	private StringBuffer paragraph;
	
	public HTMLDocumentBuilder(ChaptersNumering mChaptersNumering) {
		if (mChaptersNumering == null)
			throw new IllegalArgumentException(
					"jh Dec 10, 2011:2:06:20 AM comment");
		this.mChaptersNumering = mChaptersNumering;
		
		currentChapterLevel = 0;
		html=new StringBuffer();
		body=new StringBuffer();
		
		
		
		if(mChaptersNumering.equals(ChaptersNumering.NORMAL))
			chapterLevelStartTag="<OL type=1>\n";
		else
			chapterLevelStartTag="<OL type=none>\n";
		
		paragraph=new StringBuffer();
		body.append("<body>\n"+chapterLevelStartTag);
			
				
	}

	private String getTabs() {
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < currentChapterLevel; i++) {
			result.append("\t");
		}
		return result.toString();
	}
	private String getTabs(int level) {
		StringBuffer result = new StringBuffer();
		for (int i = 0; i < level; i++) {
			result.append("\t");
		}
		return result.toString();
	}

	@Override
	public DocumentBuilder addTitle(String _title) {
		if (_title == null)
			throw new IllegalArgumentException(
					"jh_Dec 9, 2011:19:31:29 PM title is null");
		this.title=new StringBuffer();
		
		this.title.append("<head>\n\t\t<title>" + _title + "</title>\n</head>\n");
		return this;
	}

	@Override
	public DocumentBuilder addAuthor(String author) {
		if(author==null)
			throw new IllegalArgumentException(
					"jh Dec 10, 2011:2:08:46 AM author is null");
		this.author=new StringBuffer();
		this.author.append(author);
		return this;
	}

	@Override
	public DocumentBuilder addChapter(String chapterName, int level) {
		if (chapterName == null)
			throw new IllegalArgumentException(
					"jh_Dec 9, 2011:10:37:46 PM chapterName is null");
		if (level < 0 && level < 4)
			throw new IllegalArgumentException(
					"jh_Dec 9, 2011:10:38:18 PM level is" + level
							+ " but must be some of this {0,1,2,3} ");
		

		
		String chapterTag = "h" + (level+1);
		if(currentChapterLevel==level){
			body.append(getTabs());
			body.append(getTabs()+"<LI>");
			body.append("<" + chapterTag + ">" + chapterName + "</" + chapterTag+ "><br>");
			body.append("</LI>\n");
		}
		else if(currentChapterLevel<level) {
			currentChapterLevel=level;
			body.append(getTabs()+chapterLevelStartTag);
			body.append(getTabs()+"<LI>");
			body.append("<" + chapterTag + ">" + chapterName + "</" + chapterTag+ "><br>");
			body.append("</LI>\n");
		}else
		{
			int pomLevel=currentChapterLevel;
			currentChapterLevel=level;
			
			while(currentChapterLevel<pomLevel){
				body.append(getTabs(pomLevel)+chapterLevelEndTag);
				--pomLevel;
			}
			body.append(getTabs()+chapterLevelStartTag);
			body.append(getTabs()+"<LI>");
			body.append("<" + chapterTag + ">" + chapterName + "</" + chapterTag+ "><br>");
			body.append("</LI>\n");
		}
		
		
		
		
		return this;
	}

	@Override
	public DocumentBuilder addParagraph(String paragraphText) {
		if(paragraphText==null)
			throw new IllegalArgumentException(
					"jh Dec 10, 2011:2:34:57 AM paragraph is null");
		if(paragraphText.isEmpty())
			throw new IllegalArgumentException(
					"jh Dec 10, 2011:2:38:24 AM paragraph is empty ");
		
		StringTokenizer tok = new StringTokenizer(paragraphText, "\n");

		paragraph.append(getTabs()).append("\t").append(tok.nextToken())
				.append("<br>\n");
		while (tok.hasMoreTokens()) {
			paragraph.append(getTabs()).append(tok.nextToken() + "<br>\n");
		}
		
		return this;
	}

	public String create() {
		if (title != null)
			html.append(title);
		
		while(currentChapterLevel>0){
			body.append(getTabs()+chapterLevelEndTag);
			--currentChapterLevel;
		}
		body.append(getTabs()+chapterLevelEndTag);
		body.append("</body>\n");
		html.append(body.toString());
		html.append("</html>");
		return html.toString();

	}

	void saveToFile(String path) {
		if (path == null)
			throw new IllegalArgumentException("path is null");
		if (path.isEmpty())
			throw new IllegalArgumentException("path is empty");

		FileWriter file;
		try {
			file = new FileWriter(path + ".html");
			file.write(create());
			file.close();
		} catch (IOException e) {
			System.out.println("problem with file:" + e.getMessage());
		}

	}

	@Override
	public DocumentBuilder addBulleted(String bullet) {
		// TODO Auto-generated method stub
		return this;
	}
}
